package com.sy.easycms.bo;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.sy.easycms.dao.mysql.DAO;
import com.sy.easycms.hbm.HibernateSessionFactory;
import com.sy.easycms.orm.Tag;

public class TagBO extends BaseBO {

	public TagBO() {
	}
	
	private DAO dao = new DAO();
	
	public void updateTag(int id,String tag) throws SQLException
	{
		Tag instance = super.getTagDAO().findById(id);
		instance.setTag(tag);
		getSession().update(instance);
	}
	
	public void delTag(int id) throws SQLException
	{
		Tag instance = super.getTagDAO().findById(id);
		getSession().delete(instance);
	}
	
	public void addTag(Tag instance) throws SQLException
	{ 
		Session session = getSession();
		List list = super.getTagDAO().findByTag(instance.getTag());
		if(list.size() > 0)
		{
			throw new SQLException("标签内容已存在,请勿重复添加!");
		}
		session.save(instance);
	}
	
	
	/**
	 * 不签列表
	 * @return
	 * @throws SQLException
	 */
	public List<Tag> tagList() throws SQLException
	{
//		ResultSet rs = dao.select("select * from tag");
//		List<Tag> result = new ArrayList<Tag>();
//		while(rs.next())
//		{
//			Tag tag = new Tag();
//			tag.setId(rs.getInt(0));
//			tag.setTag(rs.getString(1));
//			result.add(tag);
//		}
		Session session = getSession();
		Query query = session.createQuery("from Tag");
		
		return query.list();
	}
}
